<?php
global $Silas;?>

<?php if ($error):?>
<div id="message" class="error fade"><p><strong><?php echo $error?></strong></p></div>
<?php elseif ($message):?>
<div id="message" class="updated fade"><p><strong><?php echo $message?></strong></p></div>
<?php endif;?>

<style>
fieldset.options {
    clear:both;
    border:1px solid #ccc;
}
fieldset.options legend {
    font-family: Georgia,"Times New Roman",Times,serif;
    font-size: 22px;
}

div.album {
    float:left;
    width:200px;
    height:150px;
    margin-right:15px;
}
div.album td {
    font-size:0.9em;
}
div.album-hidden img {
    opacity:0.5;
}
</style>


<div class="wrap">
<h2 id="write-post">Photo Album Configuration</h2>
<p>
This plugin will retrieve your Flickr photosets and display them as albums on a page within this site. 
This plugin will also allow you to easily add your photos to your blog posts (or pages). 
</p>
<p><strong>Installation and Usage:</strong> Just follow the onscreen prompts to link your photo album to a Flickr account. 
To insert photos into your posts, just click the <em>Photos</em> tab in the edit screen and then click on a thumbnail. </p>

<p><em>Important:</em> After you have updated or created a new Flickr set, visit this page to update your photo album's cache with your latest photos.
If you are logged in as an administrator, then you should also see a "refresh photos" button in the top right corner of your photo album which does the same thing.
</p>

<?php if (!is_object($Silas)):?>

<div  style="float:right;width:250px;background:#eee;padding:10px;font-size:0.9em;">
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
If you find this plugin helpful, please consider donating a few dollars to support this plugin. Thanks!
<br /><br />
<input type="hidden" name="cmd" value="_s-xclick">
<input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but04.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
<input type="hidden" name="encrypted" value="-----BEGIN PKCS7-----MIIHRwYJKoZIhvcNAQcEoIIHODCCBzQCAQExggEwMIIBLAIBADCBlDCBjjELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAkNBMRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MRQwEgYDVQQKEwtQYXlQYWwgSW5jLjETMBEGA1UECxQKbGl2ZV9jZXJ0czERMA8GA1UEAxQIbGl2ZV9hcGkxHDA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FF8jermCkGcvoxLmOQOOXpKH9VPHj3a56SP8xS27lzbDvZ8TPFvcv0Afd7gGtjWYG1QkuKqs78ARtzIbSPuVngCqriAq4rZteJjWvMh4NIoL4qzXiOXdw5VId+QLUf0yVy2KLGIbw==-----END PKCS7-----">

</form>
</div>


<p>
This plugin is provided by <a href="http://www.silaspartners.com">Silas Partners</a> 
and licensed free of charge for you to use under the GPL.
This plugin is <em>unsupported</em> and comes with no official technical support. Please do not contact Silas Partners for support for this plugin.
</p>
<p>
However, you can check the following pages for the latest updates to this plugin, 
along with any unofficial technical support:<br>
<br>
<strong>Releases:</strong> <a href="http://tantannoodles.com/toolkit/photo-album/">tantannoodles.com/toolkit/photo-album/</a><br>
<strong>Support Group:</strong> <a href="http://www.flickr.com/groups/tantannoodles/">flickr.com/groups/tantannoodles</a>
</p>
<p>
<strong>RSS Updates:</strong> Subscribe to the <a href="http://feeds.feedburner.com/TanTanToolkit">TanTanToolkit feed</a> and get notified when there's an update to this plugin.
</p>



<?php endif;?>
<?php if (!$flickr_apikey || !$flickr_sharedsecret): ?>
<fieldset class="options">
<legend>Flickr API Key</legend>
<p>
Before you can setup your blog to pull in your Flickr photos, you need to get your own Flickr API key. 
</p>
<ol>
    <li>Apply for your own <a href="http://www.flickr.com/services/api/key.gne" target="_blank">Flickr API key</a>.
    If you already have an API key setup, <a href="http://www.flickr.com/services/api/keys/" target="_blank">you can use that instead</a>.
    </li>
    <li>After you have your key, click the configuration link to view the details for your new key</li>
    <li>Enter your blog's title and a short description.</li>
    <li>Change the authentication type to <b>Desktop Application</b></li>
    <li>Save</li>
    <li>Now click "edit configuration" again to view the details for newly configured API key and copy and paste
    the <b>key</b> and <b>shared secret</b> fields into the form below.<br>
    <br>
    Note that it may take a minute or two to get your key fully activated.
    </li>
    
</ol>

<table width="100%" cellspacing="2" cellpadding="5" class="editform"> 
<form method="post">
<input type="hidden" name="action" value="savekey" />
<tr valign="top">
<th width="33%" scope="row">Flickr API Key:</th> 
<td><input type="text" name="flickr_apikey" value="<?php echo $flickr_apikey ?>" size="40" /></td> 
</tr>

<tr valign="top">
<th width="33%" scope="row">Shared Secret:</th> 
<td><input type="text" name="flickr_sharedsecret" value="<?php echo $flickr_sharedsecret ?>" /></td> 
</tr>
<tr><td colspan="2">
<p class="submit">
<input type="submit" value="next &gt;" />
</p>
</td></tr>
</form>
</table>
</fieldset>

<?php elseif (!$flickrAuth):?>
<?php
    $redirect = $_SERVER['SCRIPT_URI'] . '?' . $_SERVER['QUERY_STRING'];
    $perms = "read";
    $api_sig = md5($flickr->getSharedSecret() . 
        "api_key" . $flickr->getAPIKey() . 
        "frob"  . $frob .
        "perms" . $perms);
?>
<fieldset class="options">
<legend>Flickr Settings</legend>
<p>
Your Photo Album is not currently linked to a Flickr account. 
</p>
<h3>Step 1:</h3>
<p>Login to Flickr and grant <em>read only</em> permissions to this photo album.
Once you are done, close the popup window and click the button in Step 2.
</p>
    <form method="get" action="http://flickr.com/services/auth/" target="_blank">
    <input type="hidden" name="api_key" value="<?php echo $flickr->getAPIKey()?>" />
    <input type="hidden" name="frob" value="<?php echo $frob?>" />
    <input type="hidden" name="perms" value="<?php echo $perms?>" />
    <input type="hidden" name="api_sig" value="<?php echo $api_sig?>" />
    
    <input type="submit" value="Retrieve Flickr Permissions &gt;" ?>
    </form>

<h3>Step 2:</h3>    
<p>Apply the permissions granted in Step 1 to this photo album. This step may take a couple minutes to complete (depending on how many albums you have),
since it's also going to pull down your Flickr information.</p>
    <form method="post" id="flickr">
    <input type="hidden" name="action" value="save" />
    <input type="hidden" name="frob" value="<?php echo $frob?>" />
    <input type="submit" value="Apply Permissions &gt;" />
    
    </form>

<p>
<strong>Note:</strong> You can revoke the permissions granted here in <a href="http://flickr.com/services/auth/list.gne">your Flickr access control panel</a>.
</p>

<form method="post">
<hr />
<input type="hidden" name="action" value="resetkey" />
<input type="submit" value="&lt; Reset API Key" />
</form>

</fieldset>
<?php else:?>

<fieldset class="options">
<legend>Flickr Settings</legend>



<table width="100%" cellspacing="2" cellpadding="5" class="editform"> 
<form method="post">
<input type="hidden" name="action" value="logout" />
<tr valign="top">
<th width="33%" scope="row">Retrieve Albums from this Flickr Account:</th> 
<td><a href="http://flickr.com/photos/<?php echo $user['user']['nsid']?>/"><strong><?php echo $user['user']['username']?></strong></a> &nbsp; <input type="submit" value="Remove Link &gt;" />
<br><small>Your Photo Album has been granted <em>read only</em> permissions to this Flickr account</small>
</td> 
</tr>
</form>

<form method="post">
<input type="hidden" name="action" value="savebase" />
<tr> 
<th width="33%" scope="row">Photo Album URL:</th> 
<td><? bloginfo('siteurl')?>/<input type="text" name="baseurl" value="<?php echo substr($baseurl, strlen($baseurl_pre))?>" />

</td> 
</tr> 
<tr><td></td>
    <td><small>Enter the path where you want your photo album to be shown. 
    <?php if ($baseurl):?>
    View your album: <a href="<?php echo $baseurl?>"><?php echo $baseurl?></a>
    <?php endif;?>
    </small></td>
</tr>


<tr>
<th width="33%" scope="row">Photos should link to:</th> 
<td>
<input type="radio" name="linkoptions" value="flickr" <?php echo ($linkoptions ? 'checked="checked"' : '')?> id="linkoptions1" /><label for="linkoptions1"> Flickr.com</label>
&nbsp; 
<input type="radio" name="linkoptions" value="" <?php echo (!$linkoptions ? 'checked="checked"' : '')?> id="linkoptions2" /><label for="linkoptions2"> The URL specified above</label>
<br /><small>Where you want your photos to link to, when inserted into a blog post or from the sidebar</small>
</td>

</tr>

<tr>
<th width="33%" scope="row">Flickr Sidebar Widget:</th> 
<td>
<?php if (function_exists('register_sidebar_widget')):?>

<input type="checkbox" name="showbadge" value="1" <?php echo ($showbadge ? 'checked="checked"' : '')?> id="showbadge" /><label for="showbadge"> Enable the Flickr Widget for your sidebar.</label><br>
<small>This will show your recent photos in your site's sidebar. 
<?php
global $registered_sidebars;
if (count($registered_sidebars) <= 0):?>
<br /><strong>Note:</strong> Your theme is not configured for WordPress Widgets. Here are instructions on <a href="http://automattic.com/code/widgets/themes/">how to add WordPress Widgets</a> to your existing theme.
<?php else:?>
<a href="themes.php?page=widgets/widgets.php">Configure your sidebar &gt;</a></small>
<?php endif;?>

<?php else:?>
Install the <a href="http://automattic.com/code/widgets/">WordPress Widgets plugin</a> if you want to show your recent Flickr photos in your WordPress sidebar.

<?php endif;?>

</td>
</tr>


<tr>
<th width="33%" scope="row">Private Photos:</th> 
<td>
<input type="checkbox" name="hideprivate" value="1" <?php echo ($hideprivate ? 'checked="checked"' : '')?> id="hideprivate" /><label for="hideprivate"> Hide photos that have been marked <em>Private</em>.</label>
</td>
</tr>




<tr><td colspan="2">
<p class="submit">
<input type="submit" value="save settings &gt;" />
</p>
</td></tr>


</form>

<tr>

</table>


</fieldset>






<fieldset class="options">
<legend>Photo Albums</legend>

<form method="post">
<input type="hidden" name="action" value="clearcache" />
<input type="hidden" name="album" value="all" />

<input type="button" value="Organize Albums" onclick="window.open('http://www.flickr.com/photos/organize/?start_tab=sets', '_blank');return false" />
<input type="submit" value="Refresh All Albums" /><br>
<small>Your Flickr albums will be cached locally to help speed things up. Click <strong>refresh</strong> to synchronize and refresh all your albums.</small>
</form>

<hr />


<?php
$albums = $flickr->manualSort($flickr->getAlbumsActual(), $albumOrder);
if (count($albums) <= 0):?>
<p>
    <strong>Error</strong>: Please create at least one Flickr photo set, and then click the "Refresh" button above
    to see your photos.
</p>
<?php endif; ?>

<form method="post">
<input type="hidden" name="action" value="savealbumsettings" />

<?
$i=1;
if (is_array($albums)) foreach ($albums as $id => $album) {
    if (false && $i == 1 ) { 
        $flickr->startClearCache();
        $photos = $flickr->getPhotos($id);
        $flickr->doneClearCache();
    } else {
        $photos = $flickr->getPhotos($id);
    }
    ?>
    <div class="album <?php echo (in_array($album['id'], $hideAlbums) ? 'album-hidden"' : '') ?>" id="album-<?php echo $album['id']?>">
    <table cellpadding="5">
    <tr valign="top">
    <td><a href="<?php echo $baseurl?>album/<?php echo $album['id']?>/<?php echo $album['pagename']?>"><img src="<?php echo $photos[$album['primary']]['sizes']['Square']['source'];?>" border="0" /></a></td>
    <td width="100%">
    <strong><a href="<?php echo $baseurl?>album/<?php echo $album['id']?>/<?php echo $album['pagename']?>"><?php echo $album['title']?></a></strong> <br />
    <?php echo count($photos);?> photos<br>
    <input type="checkbox" name="hideAlbum[]" <?php echo (in_array($album['id'], $hideAlbums) ? 'checked="checked"' : '') ?> value="<?php echo $album['id']?>" id="album-hide-<?php echo $album['id']?>" /><label for="album-hide-<?php echo $album['id']?>">hide album</label><br>
    <input type="checkbox" name="clearAlbum[]" value="<?php echo $album['id']?>" id="album-cache-<?php echo $album['id']?>" /><label for="album-cache-<?php echo $album['id']?>">clear cache</label><br>
    order: <input type="text" size="2" name="albumOrder[<?php echo $album['id']?>]" value="<?php echo $albumOrder[$album['id']] ? $albumOrder[$album['id']] : '0'?>" />
    
    </td>
    </tr>
    
    </table>
    </div>
    <?php
} //foreach
/*
    <fform method="post" class="album">
    <input type="hidden" name="action" value="clearcache" />
    <input type="hidden" name="album" value="<?php echo $album['id']?>" />
    <img src="<?php echo $photos[$album['primary']]['sizes']['Square']['source'];?>" /><br>
    <strong><a href="<?php echo $baseurl?>album/<?php echo $album['id']?>/<?php echo $album['pagename']?>"><?php echo $album['title']?></a></strong> 
    <input type="submit" value="refresh photos &gt;" />
    <br />
    </fform>
*/
?>
<p class="submit" style="clear:both;">
<input type="submit" value="save settings &gt;" />
</p>
</form>
</fieldset>
<fieldset class="options">
<legend>Group Photos</legend>
<form method="post">
<input type="hidden" name="action" value="cleargroupcache" />
<input type="hidden" name="group" value="all" />

<input type="button" value="Organize Groups" onclick="window.open('http://flickr.com/photos/organize/?start_tab=groups', '_blank');return false;" />
<input type="submit" value="Refresh All Groups" /><br>
<small>Your Flickr groups will be cached locally to help speed things up.
Click <strong>refresh</strong> to synchronize and refresh all your groups.</small>
<hr />
</form>

<form method="post">
<input type="hidden" name="action" value="savegroupsettings" />

<?php
$groups = $flickr->manualSort($flickr->getGroupsActual(), $groupOrder);

foreach ($groups as $group):
?>
<div class="group album <?php echo (in_array($group['id'], $hideGroups) ? 'album-hidden"' : '') ?>" id="group-<?php echo $group['id']?>">
<table cellpadding="5">
    <tr valign="top">
    <td><a href="<?php echo $baseurl?>group/<?php echo $group['id']?>/<?php echo $group['pagename']?>"><img src="<?php echo $group['iconurl']?>" /></a></td>
    <td width="100%">
    <strong><a href="<?php echo $baseurl?>group/<?php echo $group['id']?>/<?php echo $group['pagename']?>"><?php echo $group['name']?></a></strong><br>
    <?php echo $group['numphotos'];?> photos<br>
    <input type="checkbox" name="hideGroup[]" <?php echo (in_array($group['id'], $hideGroups) ? 'checked="checked"' : '') ?> value="<?php echo $group['id']?>" id="group-hide-<?php echo $group['id']?>" /><label for="group-hide-<?php echo $group['id']?>">hide group</label><br>
    <input type="checkbox" name="clearGroup[]" value="<?php echo $group['id']?>" id="group-cache-<?php echo $group['id']?>" /><label for="group-cache-<?php echo $group['id']?>">clear cache</label><br>
    order: <input type="text" size="2" name="groupOrder[<?php echo $group['id']?>]" value="<?php echo $groupOrder[$group['id']] ? $groupOrder[$group['id']] : '0'?>" />
    </td></tr>
</table>

</div>

<?php endforeach;?>

<a href="#" onclick="return hideAllGroups()">toggle group hiding</a>
<p class="submit" style="clear:both;">
<input type="submit" value="save settings &gt;" />
</p>
<script>
function hideAllGroups() {
    var inputs = document.getElementsByTagName('input')
    for (var i=0; i<inputs.length; i++) {
        if (inputs[i].name == 'hideGroup[]') {
            inputs[i].checked = !inputs[i].checked
        }
    }
    return false
}
</script>
</form>

</fieldset>

<?php endif; //flickr auth
?>



</div>